{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 将int，str，和float分开【简单】"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "给定一个列表，其中由整数和浮点数和字符串。\n",
    "\n",
    "将int放在列表前面位置，float放在列表后面位置，将str放在中间位置。\n",
    "\n",
    "在原列表上操作。\n",
    "\n",
    "例如：arr = [1,“hello”,0.1,3,0.4,5,\"world\"]\n",
    "\n",
    "输出为\n",
    "\n",
    " [1,5,3,“hello”,\"world\",0.4,0.1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#==============================答题区==============================#\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "#==============================答题区==============================#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#==============================提示区==============================#\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "#  提示：可以通过交换的方式，从首尾同时操作\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "#==============================提示区==============================#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "#==============================答案区==============================#\n",
    "# 解题思路：首尾各用一个变量记录下一个int和float要放置的位置\n",
    "# 扫描数组，如果是int就交换到前面，如果是float就交换到后面\n",
    "\n",
    "arr = [1,\"hello\",0.1,3,0.4,5,\"world\"]\n",
    "\n",
    "def f(arr):\n",
    "    if len(arr)<=1:\n",
    "        return(arr)\n",
    "    i,j = 0,len(arr)-1\n",
    "    k = 0\n",
    "    while k<=j:\n",
    "        if isinstance(arr[k],int):\n",
    "            arr[i],arr[k] = arr[k],arr[i]\n",
    "            i = i+1\n",
    "            k = max(k,i)\n",
    "        elif isinstance(arr[k],float):\n",
    "            arr[j],arr[k] = arr[k],arr[j]\n",
    "            j = j-1\n",
    "        else:\n",
    "            k = k+1\n",
    "        print(arr,\"i=%d,j=%d,k=%d\"%(i,j,k))\n",
    "    return(arr)\n",
    "#==============================答案区==============================#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 'hello', 0.1, 3, 0.4, 5, 'world'] i=1,j=6,k=1\n",
      "[1, 'hello', 0.1, 3, 0.4, 5, 'world'] i=1,j=6,k=2\n",
      "[1, 'hello', 'world', 3, 0.4, 5, 0.1] i=1,j=5,k=2\n",
      "[1, 'hello', 'world', 3, 0.4, 5, 0.1] i=1,j=5,k=3\n",
      "[1, 3, 'world', 'hello', 0.4, 5, 0.1] i=2,j=5,k=3\n",
      "[1, 3, 'world', 'hello', 0.4, 5, 0.1] i=2,j=5,k=4\n",
      "[1, 3, 'world', 'hello', 5, 0.4, 0.1] i=2,j=4,k=4\n",
      "[1, 3, 5, 'hello', 'world', 0.4, 0.1] i=3,j=4,k=4\n",
      "[1, 3, 5, 'hello', 'world', 0.4, 0.1] i=3,j=4,k=5\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1, 3, 5, 'hello', 'world', 0.4, 0.1]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](../Python与算法之美.jpg)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
